package net.shopxx.action.admin;

import javax.annotation.Resource;

import net.shopxx.bean.Pager;
import net.shopxx.bean.Pager.OrderType;
import net.shopxx.entity.Branch;
import net.shopxx.service.BranchService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.UrlValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 分店
 */

@ParentPackage("admin")
public class BranchAction extends BaseAdminAction {

	private static final long serialVersionUID = 1176011200723654304L;

	private Branch branch;

	@Resource
	private BranchService branchService;
	
	// 是否Name已存在 ajax验证
	public String checkName() {
		String oldValue = getParameter("oldValue");
		String newValue = branch.getName();
		if (branchService.isUnique("name", oldValue, newValue)) {
			return ajaxText("true");
		} else {
			return ajaxText("false");
		}
	}
	// 是否displayName已存在 ajax验证
	public String checkDisplayName() {
		String oldValue = getParameter("oldValue");
		String newValue = branch.getDisplayName();
		if (branchService.isUnique("displayName", oldValue, newValue)) {
			return ajaxText("true");
		} else {
			return ajaxText("false");
		}
	}
	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		branch = branchService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		if (pager == null) {
			pager = new Pager();
			pager.setOrderType(OrderType.asc);
			pager.setOrderBy("name");
		}
		pager = branchService.findByPager(pager);
		return LIST;
	}

	// 删除
	public String delete() {
		branchService.delete(ids);
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "branch.name", message = "分店标示不允许为空!")
		}, 
		requiredFields = {
			@RequiredFieldValidator(fieldName = "branch.displayName", message = "分店显示名称不允许为空!")
		},
		urls = {
			@UrlValidator(fieldName = "branch.url", message = "网址格式错误!")
		}
	)
	@InputConfig(resultName = "error")
	public String save() {
//		branch.setDeliveryTypeSet(null);
		branchService.save(branch);
		redirectionUrl = "branch!list.action";
		return SUCCESS;
	}

	// 更新
	@Validations(
		requiredStrings = { 
				@RequiredStringValidator(fieldName = "branch.name", message = "分店标示不允许为空!")
		}, 
		requiredFields = {
				@RequiredFieldValidator(fieldName = "branch.displayName", message = "分店显示名称不允许为空!")
		},
		urls = {
			@UrlValidator(fieldName = "branch.url", message = "网址格式错误!")
		}
	)
	@InputConfig(resultName = "error")
	public String update() {
		Branch persistent = branchService.load(id);
		BeanUtils.copyProperties(branch, persistent, new String[]{"id", "createDate", "modifyDate"});
//		BeanUtils.copyProperties(branch, persistent);
		branchService.update(persistent);
		redirectionUrl = "branch!list.action";
		return SUCCESS;
	}

	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}



}